package com.longrise.android.byjk.widget.recyclerviewadapter;

import android.view.View;
import android.view.ViewGroup;

/**
 * Created by sjz on 15/4/25.
 */
public abstract class HeaderFooterAdapter<VH extends BaseRecyclerViewAdapter.BaseRecyclerViewHolder> extends BaseRecyclerViewAdapter<VH> {

    protected OnItemClickListener mOnItemClickListener;

    protected OnItemLongClickListener mOnItemLongClickListener;

    public static final int ITEM_TYPE_HEADER = Integer.MIN_VALUE;
    public static final int ITEM_TYPE_FOOTER = Integer.MIN_VALUE + 1;
    public static final int ITEM_TYPE_OFFSET = 2;

    @Override
    protected VH onCreateNormalItemViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ITEM_TYPE_HEADER:
                return onCreateHeaderItemViewHolder(parent);
            case ITEM_TYPE_FOOTER:
                return onCreateFooterItemViewHolder(parent);
            default:
                return onCreateContentItemViewHolder(parent, viewType - ITEM_TYPE_OFFSET);
        }
    }

    @Override
    protected void onBindNormalViewHolder(VH holder, final int position) {
        if (position == 0 && ITEM_TYPE_HEADER == holder.getItemViewType()) {
            onBindHeaderView(holder, position);
        } else if (position == getContentItemCount() && ITEM_TYPE_FOOTER == holder.getItemViewType()) {
            onBindFooterView(holder, position);
        } else {
            final int pos = position - (hasHeader() ? 1 : 0);
            onBindContentView(holder, pos);
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(v, pos);
                    }
                });
            }
            if (mOnItemLongClickListener != null) {
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return mOnItemLongClickListener.onItemLongClick(v, pos);
                    }
                });
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && hasHeader()) {
            return ITEM_TYPE_HEADER;
        } else if (position == getContentItemCount() && hasFooter()) {
            return ITEM_TYPE_FOOTER;
        }
        return getContentItemViewType(position - (hasHeader() ? 1 : 0)) + ITEM_TYPE_OFFSET;
    }

    @Override
    public int getItemCount() {
        int contentItemCount = getContentItemCount();
        if (hasHeader()) {
            contentItemCount++;
        }
        if (hasFooter()) {
            contentItemCount++;
        }
        return contentItemCount;
    }

    public void reportNotifyItemInserted(int position) {
        notifyItemInserted(hasHeader() ? position + 1 : position);
    }

    public void reportNotifyItemChanged(int position) {
        notifyItemChanged(hasHeader() ? position + 1 : position);
    }

    public void reportNotifyItemRemoved(int position) {
        notifyItemRemoved(hasHeader() ? position + 1 : position);
    }

    public void reportNotifyItemRangeChanged(int positionStart, int itemCount) {
        notifyItemRangeChanged(hasHeader() ? positionStart + 1 : positionStart, itemCount);
    }

    public void reportNotifyItemRangeRemoved(int positionStart, int itemCount) {
        notifyItemRangeRemoved(hasHeader() ? positionStart + 1 : positionStart, itemCount);
    }

    protected abstract int getContentItemCount();

    protected abstract boolean hasHeader();

    protected abstract boolean hasFooter();

    protected abstract VH onCreateHeaderItemViewHolder(ViewGroup parent);

    protected abstract VH onCreateFooterItemViewHolder(ViewGroup parent);

    protected abstract VH onCreateContentItemViewHolder(ViewGroup parent, int viewType);

    protected abstract void onBindHeaderView(VH holder, int position);

    protected abstract void onBindFooterView(VH holder, int position);

    protected abstract void onBindContentView(VH holder, int position);

    protected abstract int getContentItemViewType(int position);

    public interface OnItemClickListener {
        void onItemClick(View itemView, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(View itemView, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.mOnItemLongClickListener = onItemLongClickListener;
    }
}
